package com.sxhalo.fsms.controller.api;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.sxhalo.fsms.domain.FactPatrolJobItem;
import com.sxhalo.fsms.service.IFactPatrolJobItemService;
import com.sxhalo.fsms.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.UserPatrolItemRecord;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.IUserPatrolItemRecordService;
import com.sxhalo.fsms.util.ApiResult;

@Controller
@RequestMapping("/api")
public class UserPatrolItemRecordController extends BaseController {

	@Autowired
	private IUserPatrolItemRecordService recordService;

	@Autowired
	private IFactPatrolJobItemService jobItemService;
	@ResponseBody
	@RequestMapping(value = "/patrolrecord/item", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findPatrol(Integer id, Integer jobId, Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "";

		if (id != null && jobId != null) {
			where = "record_id= " + id + " and job_id=" + jobId;
		}


		Integer count = recordService.getCount(where);
		List<FactPatrolJobItem> list = jobItemService.getItemsRecordByJobId(id,jobId);

		PageData page = new PageData(0, 9999, count, list);
		return ApiResult.retOK(req, page);


	}
	
	
	@ResponseBody
	@SysControllerLog(operationName = "新增用户巡检项目", modelName = "用户巡检项目")
	@RequestMapping(value = "/patrolrecord/item", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult savePatrol(@RequestBody UserPatrolItemRecord[] record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		for (UserPatrolItemRecord itemRecord : record) {
			itemRecord.setCreateTime(nowtime);
			itemRecord.setUpdateTime(nowtime);
			recordService.insertSelective(itemRecord);
		}
		return ApiResult.retOK(req);
	}


	//实时更新用户巡检项
	@ResponseBody
	@RequestMapping(value = "/patrolrecord/item/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveOrUpdatePatrol(@RequestBody UserPatrolItemRecord record, HttpServletRequest req) {
		recordService.insertOrUpdate(record);
		return ApiResult.retOK(req);
	}





	@ResponseBody
	@SysControllerLog(operationName = "更新用户巡检项目", modelName = "用户巡检项目")
	@RequestMapping(value = "/patrolrecord/item", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updatePatrol(@RequestBody UserPatrolItemRecord[] record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		for (UserPatrolItemRecord item : record) {
			UserPatrolItemRecord old = recordService.selectByPrimaryKey(item);
			if (old == null) {
				item.setIsWorks(1);
				item.setUpdateTime(nowtime);
				item.setCreateTime(nowtime);
				recordService.insertSelective(item);
			}else {
				item.setIsWorks(1);
				item.setUpdateTime(nowtime);
				recordService.updateByPrimaryKeySelective(item);
			}
		}
		return ApiResult.retOK(req);
	}
	
	
	@ResponseBody
	@SysControllerLog(operationName = "删除用户巡检项目", modelName = "用户巡检项目")
	@RequestMapping(value = "/patrolrecord/item", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delPatrol(Integer id, HttpServletRequest req) {
		recordService.deleteByPrimaryKey(id);
		return ApiResult.retOK(req);
	}
}
